/**
 * Created by udi on 2016/11/28.
 */


require(["jv", "vue", "mui","pop"], function () {
  var Vue = require("vue");

  if (!location.map.token) {
    jv.setToken();
  }

  var OrderStatusEnum = jv.enumer({
    waitPay: "未付款",
    paid: "已付款",
    confirmed: "已接单",
    working: "服务中",
    completed: "服务完成",
    closed: "已关闭",
    cancelled: "已取消"
  });


  var bindVue = function (data) {
    data.wuyes = [];
    data.ayis = [];

    var vm = jv.vm = new Vue({
      el: '#app',
      data: data,
      computed: {},
      methods: {
        getStatus: function (status) {
          return OrderStatusEnum.getData(status);
        },
        cancel: function (ev) {
          var btnArray = ['否', '是'];
          mui.confirm('确认要取消该订单吗？', "", btnArray, function (e) {
            if (!e.index) {
              return;
            }
            jv.post("/order/cancel", {orderId: this.id}, function (res) {
              jv.vm.$data.status = "cancelled";
            });
          });
        },
        setAmount: function () {
          //e.detail.gesture.preventDefault(); //修复iOS 8.x平台存在的bug，使用plus.nativeUI.prompt会造成输入法闪一下又没了
          var btnArray = ['取消', '确定'];
          mui.prompt('请输入要设置的价格（元）：', jv.vm.$data.total, '', btnArray, function (e) {
            if (!e.index) {
              return;
            }
            var amount = parseFloat(e.value);
            if (amount !== 0 && !amount) {
              mui.alert("请输入正确的金额");
              return;
            }
            jv.vm.$data.total = amount;
            jv.post("/wuye/setAmount", {orderId: jv.vm.$data.id, amount: amount}, function (res) {
            });
          })
        },
        setDone: function () {
          var btnArray = ['否', '是'];
          mui.confirm('确认要把该订单设置为服务完成吗？', "", btnArray, function (e) {
            if (!e.index) {
              return;
            }
            jv.post("/wuye/order/setDone", {orderId: jv.vm.$data.id}, function (res) {
              jv.vm.$data.status = "completed";
            });
          });
        },
        toWuye: function (ev) {
          var orderId = jv.vm.$data.id;
          var pop = function () {
            jv.pop({
              id: "pop_wuye",
              data: jv.vm.$data.wuyes,
              idKey: "id",
              nameKey: "name",
              changed: function (value) {
                jv.post("/wuye/toWuye", {orderId: orderId, userId: value.id}, function (res) {

                  // var order = vm.$data.items.filter(function (it) {
                  //   return it.id == orderId;
                  // })[0];
                  jv.vm.$data.wuyeName = value.name;

                });
              }
            });
          }

          if (!jv.vm.$data.wuyes.length) {
            jv.post("/wuye/getOther", function (res) {
              jv.vm.$data.wuyes = res;

              pop();
            });
            return;
          }
          pop();
        },
        toAyi: function () {
          var orderId = jv.vm.$data.id;
          var pop = function () {

            jv.pop({
              id: "ayis",
              data: jv.vm.$data.ayis,
              idKey: "id",
              nameKey: "name",
              changed: function (value) {
                jv.post("/wuye/toAyi", {orderId: orderId, userId: value.id}, function () {
                  // var order = vm.$data.items.filter(function (it) {
                  //   return it.id == orderId;
                  // });
                  jv.vm.$data.ayiName = value.name;
                });
              }
            });
          }

          var toAyiProc = function () {
            jv.post("/wuye/getAyis", {}, function (res) {
              if (!res || !res.length) {
                mui.alert("还没有阿姨信息，请添加阿姨", "&nbsp;");
                return;
              }
              jv.vm.$data.ayis = res;
              pop();
            });
          }
          //如果有阿姨，则提示
          if (!this.ayis.length) {
            toAyiProc();
            return;
          }

          pop();
          // var btnArray = ['否', '是'];
          // mui.confirm('当前已有阿姨在处理，是否要改变？', '', btnArray, function (e) {
          //   if (e.index == 1) {
          //     toAyiProc();
          //   }
          // });
        }
      }
    });
  }

  jv.post("/wuye/order_detail", {orderId: location.map.id}, bindVue);

});
